package com.docshell.service;

import com.docshell.common.DisplayManager;
import com.docshell.common.SessionManager;
import com.docshell.constants.DocShellConstants;
import com.docshell.exceptions.DocShellException;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.common.DfException;

public class ApiHandler {

	private String m_Command = null;
	private String m_Arguments = null;
	private String m_Value = null;
	private int m_ApiType = 0;
	
	public void setCommand(String m_Command) {
		this.m_Command = m_Command;
	}
	public String getCommand() {
		return m_Command;
	}
	public void setArguments(String m_Arguments) {
		this.m_Arguments = m_Arguments;
	}
	public String getArguments() {
		return m_Arguments;
	}
	public void setValue(String m_Value) {
		this.m_Value = m_Value;
	}
	public String getValue() {
		return m_Value;
	}
	public void setApiType(int m_ApiType) {
		this.m_ApiType = m_ApiType;
	}
	public int getApiType() {
		return m_ApiType;
	}
	public boolean execAPI() throws DfException, DocShellException {
		boolean result = false;
		try {
			IDfSession dfSession = SessionManager.getSession();
			if (getApiType()==DocShellConstants.SET_API_TYPE) {
				result = dfSession.apiSet(getCommand(), getArguments(), getValue());
			}
			else if (getApiType()==DocShellConstants.GET_API_TYPE) {
				String lsResult = dfSession.apiGet(getCommand(), getArguments());
				DisplayManager.writeLine(lsResult);
			}
			else if (getApiType()==DocShellConstants.EXEC_API_TYPE) {
				result = dfSession.apiExec(getCommand(), getArguments());
			}
		}
		catch (DfException dfe) {
			throw dfe;
		}
		catch (Exception ex) {
			throw new DocShellException(DocShellException.UNKNOWN_EXCEPTION, ex.toString());
		}
		return result;
	}
	
}
